package com.tanhua.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.mapper.UserInfoMapper;
import com.tanhua.sso.pojo.UserInfo;
import com.tanhua.sso.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserInfoServiceImpi implements UserInfoService{

    @Autowired
    private UserInfoMapper userInfoMapper;

    //新增用户信息
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfoVo select(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        UserInfoVo userInfoVo=new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return userInfoVo;
    }

    @Override
    public void updateUserinfoVo(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo selectUser(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }

    //分页条件查询好友信息
    @Override
    public Page<UserInfo> page(Integer page, Integer pagesize, String keyword, List<Long> listId) {
        Page<UserInfo> userInfoPage=new Page<>(page,pagesize);
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ObjectUtil.isNotEmpty(listId),UserInfo::getId,listId).like(StrUtil.isNotBlank(keyword),UserInfo::getNickname,keyword);
        Page<UserInfo> userInfoPage1 = userInfoMapper.selectPage(userInfoPage, lambdaQueryWrapper);
        return userInfoPage1;
    }

    //id加性别查询
    @Override
    public List<UserInfo> selectIds(List<Long> listId, String gender) {
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UserInfo::getId,listId).eq(UserInfo::getGender,gender);
        List<UserInfo> userInfoList = userInfoMapper.selectList(lambdaQueryWrapper);
        return userInfoList;
    }


    //用户数据翻页
    @Override
    public Page<UserInfo> selectPage(Integer page, Integer pagesize) {
        Page<UserInfo> userInfoPage=new Page<>(page,pagesize);
        Page<UserInfo> userInfoPage1 = userInfoMapper.selectPage(userInfoPage, null);
        return userInfoPage1;
    }


}
